﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_role_detail : SystemPage.UIPage
    {
        private tcdc_mst_role_detail objRoleDetail;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.RoleDetailAdd1.GridStore = StoreRoleDetail;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objRoleDetail = new tcdc_mst_role_detail();
            objRoleDetail.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreRoleDetail.DataSource = GetRoleDetail();
                StoreRoleDetail.DataBind();
            }
        }

        protected DataTable GetRoleDetail()
        {
            DataTable dt = objRoleDetail.getByObject(new tcdc_mst_role_detail());
            return dt;
        }

        protected void AddRoleDetail(object sender, DirectEventArgs e)
        {
            RoleDetailAdd1.Show();
        }

        protected void UpdateRoleDetail(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["RoleDetailValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string role_detail_id = string.Empty;
            string role_id = string.Empty;
            string function_id = string.Empty;
            string function_view = string.Empty;
            string function_add = string.Empty;
            string function_edit = string.Empty;
            string function_delete = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                role_detail_id = row["role_detail_id"].ToString();
                role_id = row["role_id"].ToString();
                function_id = row["function_id"].ToString();
                function_view = row["function_view"].ToString();
                function_add = row["function_add"].ToString();
                function_edit = row["function_edit"].ToString();
                function_delete = row["function_delete"].ToString();
            }

            objRoleDetail.role_detail_id = Convert.ToInt32(role_detail_id);
            objRoleDetail.role_id = Convert.ToInt32(role_id);
            objRoleDetail.function_id = Convert.ToInt32(function_id);
            objRoleDetail.function_view = function_view;
            objRoleDetail.function_add = function_add;
            objRoleDetail.function_edit = function_edit;
            objRoleDetail.function_delete = function_delete;
            objRoleDetail.updateData(objRoleDetail);
            StoreRoleDetail.DataSource = GetRoleDetail();
            StoreRoleDetail.DataBind();
        }

        protected void DeleteRoleDetail(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteRoleDetail();
        }

        protected void StoreRoleDetail_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreRoleDetail.DataSource = GetRoleDetail();
            StoreRoleDetail.DataBind();
        }

        protected void ExportExcelRoleDetail(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=RoleDetail.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteRoleDetail()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataRoleDetail()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataRoleDetail()
        {
            RowSelectionModel sm = this.GridPanelRoleDetail.SelectionModel.Primary as RowSelectionModel;
            string role_detail_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                role_detail_id = row.RecordID;
            }
            objRoleDetail.deleteData(role_detail_id);
            StoreRoleDetail.DataSource = GetRoleDetail();
            StoreRoleDetail.DataBind();
        }
    }
}

